package com.csust.travel.mapper;

import com.csust.travel.entity.Books;
import com.csust.travel.entity.Comment;
import com.csust.travel.entity.Recomment;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface CommentMapper {


    @Insert("INSERT INTO comment (book_name, transaction_date, rate, content, book_img, img_list,order_id,reviewer) " +
            "VALUES (#{book_name}, #{transaction_date}, #{rate}, #{content}, #{book_img}, #{imgListForDb},#{order_id},#{reviewer})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void saveItem(Comment comment);

    @Insert("INSERT INTO recomment (book_name, transaction_date, rate, content, book_img, img_list,order_id,comment_id) " +
            "VALUES (#{book_name}, #{transaction_date}, #{rate}, #{content}, #{book_img}, #{imgListForDb},#{order_id},#{comment_id})")
    void saveReItem(Recomment recomment);

    @Select("select * from comment order by comment_time desc")
     List<Comment> findAllItem();

    @Select("select * from comment where id = #{id}")
    List<Comment> findPageById(Integer id);

    @Delete("delete from comment where id = #{id}")
    void delItem(Integer id);
    @Select("select * from comment where id = #{string} or book_name like concat('%',#{string},'%') or content like concat('%',#{string},'%') ")
    List<Comment> findByQuery(String string);

    @Select("select * from comment where ( book_name like concat('%',#{string},'%') or content like concat('%',#{string},'%')) and rate = #{na}")
    List<Comment> findByFilters(String string, Integer na);

    @Select("select * from comment where rate = #{nationality}")
    List<Comment> findBooksByNationality(Integer nationality);

    @Select("<script>" +
            "SELECT c.* FROM comment c " +
            "INNER JOIN transactions t ON c.id = t.comment_id " +
            "WHERE t.seller_id = #{myComment} " +
            "AND t.hasCommented = 1" +
            "</script>")
    List<Comment> findCommentMy(Integer myComment);

    @Select("select * from comment where reviewer = #{myComment}")
    List<Comment> findMyComment(Integer myComment);

    @Update("update comment set hasRecommented = 1 where id = #{item}")
    void setHasRecommented(Integer item);

    @Select("select * from recomment where comment_id = #{id}")
    Recomment getRecomment(Integer id);

    @Select("SELECT t.seller_id " +
            "FROM comment c " +
            "JOIN transactions t ON c.order_id = t.id " +
            "WHERE c.order_id = #{order_id}")
    Integer getUser_id(Integer order_id);

    @Insert("INSERT INTO statistics (user_id, comments)\n" +
            "VALUES (#{user_id}, 1)\n" +
            "ON DUPLICATE KEY UPDATE comments = comments + 1; ")
    void addComment(Integer user_id);

}
